#include "journal.hh"
#include <iostream>
#include <stdexcept>

namespace ngintoy
{
    namespace utils
    {
        journal::journal(journal_kind kind, std::string text)
            : kind{kind}, kind_text{std::move(text)}
        {
            if (kind_text.size() != 5) {
                throw std::logic_error{
                    "kind text to journal should be exactly of length 5, "
                    "but got [" + kind_text + "]"};
            }
        }

        auto journal::writeline(std::string const& line) -> self &
        {
            switch (kind) {
                case journal_kind::info:
                    std::cerr << "\e[1m[ " << kind_text << " ]\e[0m " << line << "\n";
                    break;
                case journal_kind::warn:
                    std::cerr << "\e[33m[ " << kind_text << " ] " << line << "\e[0m\n";
                    break;
                case journal_kind::error:
                    std::cerr << "\e[30;41m  " << kind_text << "  \e[0m \e[31m" << line << "\e[0m\n";
                    break;
                case journal_kind::begin:
                    std::cerr << "\e[30;42m  " << kind_text << "  \e[0m \e[32m" << line << "\e[0m\n";
                    break;
                case journal_kind::end:
                    std::cerr << "\e[30;44m  " << kind_text << "  \e[0m \e[34m" << line << "\e[0m\n";
                    break;
            }
            return *this;
        }
    }
}

